package com.changgou.order.controller;

import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.order.config.TokenDecode;
import com.changgou.order.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @description:
 * @author: 大佬味的小男孩
 * @date: 2020-08-01 14:42
 **/
@RestController
@RequestMapping("cart")
public class CartController {

    @Autowired
    private CartService cartService;


    @Autowired
    private TokenDecode tokenDecode;


    /**
     * @param skuId
     * @param num
     * @return com.changgou.entity.Result
     * @description: 根据skuId和num（商品数量）添加到购物车
     * @author 大佬味的小男孩
     * @date 2020/08/01 16:48
     */
    @GetMapping("addCart")
    public Result addCart(@RequestParam("skuId") String skuId, @RequestParam("num") Integer num) {
       // String username = "cyj";
        String username = tokenDecode.getUserInfo().get("username");
        cartService.addcart(skuId, num, username);
        return new Result(true, StatusCode.OK, "加入购物车成功");
    }


    /**
     * @param
     * @return java.util.Map
     * @description: 查询购物车数据
     * @author 大佬味的小男孩
     * @date 2020/08/01 16:49
     */
    @GetMapping("list")
    public Map list() {
        //String username = "cyj";
        String username = tokenDecode.getUserInfo().get("username");
        Map map = cartService.list(username);
        return map;
    }

}
